package com.bkd.dreams.client.volunteer;

import java.util.Date;
import java.util.List;

import com.bkd.dreams.client.ComponentRenderer;
import com.bkd.dreams.client.event.SignUpActionCell;
import com.bkd.dreams.client.event.VolunteerEventDTO;
import com.bkd.dreams.client.event.service.VolunteerEventService;
import com.bkd.dreams.client.volunteer.service.VolunteerService;
import com.bkd.dreams.client.volunteer.service.VolunteerServiceAsync;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class VolunteerTable extends ComponentRenderer {

	private VerticalPanel usersPanel = new VerticalPanel();
	private CellTable<VolunteerDTO> usersTable = new CellTable<VolunteerDTO>();

	@Override
	public void loadTable(TabPanel tabPanel) {

		TextColumn<VolunteerDTO> nameColumn = new TextColumn<VolunteerDTO>() {
			@Override
			public String getValue(VolunteerDTO volunteer) {
				return volunteer.getName();
			}
		};
		TextColumn<VolunteerDTO> emailColumn = new TextColumn<VolunteerDTO>() {
			@Override
			public String getValue(VolunteerDTO volunteer) {
				return volunteer.getEmail();
			}
		};
		DateCell twinsDateCell = new DateCell();
		Column<VolunteerDTO, Date> twinsTrainingColumn = new Column<VolunteerDTO, Date>(twinsDateCell) {
			@Override
			public Date getValue(VolunteerDTO volunteer) {
				return volunteer.getTwinsTraining();
			}
		};
		DateCell twinsMgrDateCell = new DateCell();
		Column<VolunteerDTO, Date> twinsMgrTrainingColumn = new Column<VolunteerDTO, Date>(twinsMgrDateCell) {
			@Override
			public Date getValue(VolunteerDTO volunteer) {
				return volunteer.getTwinsTraining();
			}
		};
		DateCell vikesDateCell = new DateCell();
		Column<VolunteerDTO, Date> vikesTrainingColumn = new Column<VolunteerDTO, Date>(vikesDateCell) {
			@Override
			public Date getValue(VolunteerDTO volunteer) {
				return volunteer.getTwinsTraining();
			}
		};
		DateCell vikesMgrDateCell = new DateCell();
		Column<VolunteerDTO, Date> vikesMgrTrainingColumn = new Column<VolunteerDTO, Date>(vikesMgrDateCell) {
			@Override
			public Date getValue(VolunteerDTO volunteer) {
				return volunteer.getTwinsTraining();
			}
		};
		
		ToggleAdministratorActionCell adminActionCell = new ToggleAdministratorActionCell(
				 new ActionCell.Delegate<VolunteerDTO>() {
					public void execute(VolunteerDTO volunteer) {
						toggleAdministrator(volunteer);
					}

				});
		Column<VolunteerDTO, VolunteerDTO> toggleAdminColumn = new Column<VolunteerDTO, VolunteerDTO>(adminActionCell) {
			public VolunteerDTO getValue(VolunteerDTO volunteer) {
				return volunteer;
			}
		};
		
		usersTable.addColumn(nameColumn, "Name");
		usersTable.addColumn(emailColumn, "E-mail");
		usersTable.addColumn(twinsTrainingColumn, "Twins Training");
		usersTable.addColumn(twinsMgrTrainingColumn, "Twins Manager Training");
		usersTable.addColumn(vikesTrainingColumn, "Vikes Training");
		usersTable.addColumn(vikesMgrTrainingColumn, "Vikes Manager Training");
		usersTable.addColumn(toggleAdminColumn, "Administrator");
		
		usersPanel.add(usersTable);

		tabPanel.add(usersPanel, "Volunteers");

		refreshTable();
	}

	protected void refreshTable() {
	    VolunteerServiceAsync volService = GWT.create(VolunteerService.class);

		// Set up the callback object.
		AsyncCallback<List<VolunteerDTO>> callback = new AsyncCallback<List<VolunteerDTO>>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}
			public void onSuccess(List<VolunteerDTO> result) {
				updateUsersTable(result);
			}
		};

		// Make the call to the stock price service.
		volService.getAllVolunteers(callback);
	}
	
	protected void updateUsersTable(List<VolunteerDTO> volunteers) {
		usersTable.setRowCount(volunteers.size(),true);
		usersTable.setRowData(0, volunteers);
	}
	
	protected void toggleAdministrator(VolunteerDTO volunteer) {
		VolunteerServiceAsync volService = GWT.create(VolunteerService.class);

		// Set up the callback object.
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {
			public void onFailure(Throwable error) {
				handleError(error);
			}

			public void onSuccess(Void result) {
				refreshTable();
			}
		};

		volService.toggleAdministrator(volunteer, callback);
	}
}
